{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9e5fcfe6",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> while循环：While True →无限循环\n",
    ">> A.指定次数，while True "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "505c54f9",
   "metadata": {},
   "source": [
    "# 元组（tuple）\n",
    "> 1.元组的基础内容（形态、定义）  \n",
    "> 2.元组的性质    \n",
    ">> 1.有序性（indexing，slicing）   \n",
    ">> 2.<font color=\"red\"> **不可变性**</font>  \n",
    "> 3.元组的一般方法    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "47e7fc45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1800001, 1800002, 1800003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网三_学号 = (1800001,1800002,1800003)\n",
    "print(网三_学号,type(网三_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "003a3759",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(190001, 190002, 190003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网二_学号 = 190001,190002,190003\n",
    "print(网二_学号,type(网三_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "82b476a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "190001"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0b80b0c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(190002, 190003)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c5ed95dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=7, tm_sec=5, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组的实际案例\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bcdb2010",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tuple_test = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_test,type(tuple_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a2ad077f",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_test[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e9690bdd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4, 100], [3, 4, 5, 6]) <class 'tuple'> 1801247422016\n",
      "[1, 2, 3, 4, 100] 1801239559744\n"
     ]
    }
   ],
   "source": [
    "print(tuple_test,type(tuple_test),id(tuple_test))\n",
    "print(tuple_test[0],id(tuple_test[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "18fba34a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一般情况下，元组里面存放的是 常量（不可变的） 何为常量: 数字，字符串\n",
    "#                      不存放变量，如，列表，字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5018e9fa",
   "metadata": {},
   "source": [
    "# 集合（sets)  \n",
    "> 1.集合的基本概念  \n",
    ">> 1.性质：没有重复项  \n",
    ">> 2.无序性  \n",
    "> 2.集合的一般方法  \n",
    ">> 1.集合的运算：&，\n",
    ">> 2.集合的一般方法：intersection  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "628cf54a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'orange', 'apple', 'orange', 'banana', 'banana']\n",
      "{'apple', 'orange', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "basket_lists = ['apple', 'orange', 'apple','orange','banana','banana']\n",
    "basket_sets = {'apple', 'orange', 'apple','orange','banana','banana'}\n",
    "print(basket_lists)\n",
    "print(basket_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9a0a5a1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1801,1802,1803,1807,1809}\n",
    "电竞社 = {1801,1802,1804,1805,1808}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e9b2556",
   "metadata": {},
   "source": [
    "* 集合的与运算，交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d58f4d45",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 & 电竞社"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35bcaa00",
   "metadata": {},
   "source": [
    "* 集合的或运算，并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0dd1d0a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1804, 1805, 1807, 1808, 1809}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 | 电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a89f3248",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社 | 电竞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1672aaf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1804, 1805, 1807, 1808, 1809}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.union(电竞社)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c56f519",
   "metadata": {},
   "source": [
    "# 3.字典（dict）\n",
    "> 1.字典的基本概念：\n",
    ">> 1.字典的性质：\n",
    ">>> a.无序性  \n",
    ">>> b.keys是不可变的  \n",
    ">>> c.keys是唯一的  \n",
    ">> 2.字典的形态  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "094934af",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班 = {\n",
    "    1800001:\"Maarry\",\n",
    "    1800002:\"Jack\",\n",
    "    1800003:\"周杰伦\",\n",
    "    1800004:\"Rose\"    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8ee0bd2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: 'Maarry', 1800002: 'Jack', 1800003: '周杰伦', 1800004: 'Rose'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2f39409c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 因为学号是唯一的，可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2178ccac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: 'Maarry', 1800002: 'Jack', 1800003: '周杰伦', 1800004: 'Rose', 1800005: 'Rose'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新三班 = {\n",
    "    1800001:\"Maarry\",\n",
    "    1800002:\"Jack\",\n",
    "    1800003:\"周杰伦\",\n",
    "    1800004:\"Rose\",\n",
    "    1800005:\"Rose\"\n",
    "} \n",
    "print(网新三班,type(网新三班))  # 可以重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ea395cea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Rose'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的取值 ，通过keys进行字典的取值，unlike 列表、元组和字符串通过index取值\n",
    "网新三班[1800004]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "23061682",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新四班 = {\n",
    "    1800001:[\"Maarry\",\"广州\",\"男\",\"19\",\"聊天记录\",\"朋友圈\"],\n",
    "    1800002:\"Jack\",\n",
    "    1800003:\"周杰伦\",\n",
    "    1800004:\"Rose\",\n",
    "    1800005:\"Rose\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0a3f2090",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Maarry', '广州', '男', '19', '聊天记录', '朋友圈']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班[1800001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38398a56",
   "metadata": {},
   "source": [
    "## 3.2字典的一般方法  \n",
    "> 1. 网新四班 = {  \n",
    "    1800001:[\"Maarry\",\"广州\",\"男\",\"19\",\"聊天记录\",\"朋友圈\"],  \n",
    "    1800002:\"Jack\",  \n",
    "    1800003:\"周杰伦\",  \n",
    "    1800004:\"Rose\",  \n",
    "    1800005:\"Rose\"  \n",
    "}    \n",
    "> 2.用dict（）的方法创建字典  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1a7a2270",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用dict()方法创建字典\n",
    "网新四班 = dict(\n",
    "    stu1800001=\"李一峰\",\n",
    "    stu1800002=\"Jack\",\n",
    "    stu1800003=\"Marry\",\n",
    "    stu1800004=\"周杰伦\",\n",
    "\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "467ace35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c4ecedeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方式创建字典 : dict[key] = value\n",
    "\n",
    "网新一班 = {}\n",
    "网新一班[\"stu1800001\"] = '李一峰'\n",
    "网新一班[\"stu1800002\"] = 'Jack'\n",
    "网新一班[\"stu1800003\"] = 'Marry'\n",
    "网新一班[\"stu1800004\"] = '周杰伦'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "02dd2643",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班_keys = [\"stu1800001\",\"stu1800002\",\"stu1800003\",\"stu1800004\"]\n",
    "网新一班_values = ['李一峰','Jack','Marry','周杰伦']\n",
    "网新一班 = {}\n",
    "for i in range(4):\n",
    "    网新一班[网新一班_keys[i]] = 网新一班_values[i]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2fa2a661",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccd5e0c3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
